package com.forlove.app.qselect.answer.ui.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.forlove.app.R;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.qselect.friend.adapter.SetAnswerFriendAdapter;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 指定题目人选
 * Created by yjl on 2017/12/5.
 */

public class SetAnswerFriendFragment extends BaseCandidateFragment {
    private RecyclerView recycler_answer_friend;
    private SetAnswerFriendAdapter setAnswerFriendAdapter;
    private RelativeLayout view_no_data;
    private String queryStr;

    @Override
    protected int getLayoutId() {
        return R.layout.layout_set_anwser_firend;
    }

    private long time = 0;

    @Override
    protected void initViews() {
        view_no_data = findViewById(R.id.view_no_data);
        recycler_answer_friend = findViewById(R.id.recycler_answer_friend);
        if (setAnswerFriendAdapter == null) {
            setAnswerFriendAdapter = new SetAnswerFriendAdapter(getContext(), R.layout.item_add_friends, mineFriendEntities, basePresenter);
        }
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        recycler_answer_friend.setLayoutManager(gridLayoutManager);
        recycler_answer_friend.setAdapter(setAnswerFriendAdapter);
        setAnswerFriendAdapter.hideEmptyView();
        setAnswerFriendAdapter.hideFooterView();
        setAnswerFriendAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (System.currentTimeMillis() - time < 1000) {
                    time = System.currentTimeMillis();
                    return;
                }
                time = System.currentTimeMillis();
                MineFriendEntity mineFriendEntity = null;
                if (StringUtils.isNotEmpty(queryStr) && queryFriend.size() > position) {
                    mineFriendEntity = queryFriend.get(position);
                } else if (position < mineFriendEntities.size()) {
                    mineFriendEntity = mineFriendEntities.get(position);
                }

                if (mineFriendEntity != null) {
                    mineFriendEntity.isSelected = true;
                }
                setAnswerFriendAdapter.notifyDataSetChanged();
                if (selectFriendListener != null) {
                    List<MineFriendEntity> mineFriendEntities = new ArrayList<>();
                    mineFriendEntities.add(mineFriendEntity);
                    selectFriendListener.selectedFirend(mineFriendEntities, position_choice_friend);
                    friendHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            selectFriendListener.closeDialog();
                        }
                    }, 500);
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

//        showEmptyView();
        updateView();
    }

    private void showEmptyView() {
        if (mineFriendEntities.size() == 0) {
            view_no_data.setVisibility(View.VISIBLE);
        } else {
            view_no_data.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void inputQuery(String queryStr) {
        this.queryStr = queryStr;
        queryFriend.clear();
        queryFriend.addAll(getLocalSearchFriend(this.queryStr));
        if (StringUtils.isNotEmpty(queryStr) && setAnswerFriendAdapter != null) {
            setAnswerFriendAdapter.setQueryStr(queryStr);
            setAnswerFriendAdapter.updateDatas(queryFriend);
        } else if (setAnswerFriendAdapter != null) {
            List<MineFriendEntity> list = new ArrayList<>();
            list.addAll(mineFriendEntities);
            mineFriendEntities.clear();
            mineFriendEntities.addAll(FriendManager.sortRecommend(list));
            setAnswerFriendAdapter.updateDatas(mineFriendEntities);
        }
    }

    @Override
    public void cancelQuery() {
        this.queryStr = "";
        if (queryFriend != null) {
            queryFriend.clear();
        }
        if (setAnswerFriendAdapter != null) {
            setAnswerFriendAdapter.setQueryStr(queryStr);
        }
    }

    @Override
    public void onPageSelect(int position) {
        if (position == position_choice_friend) {
            cancelQuery();
            updateView();
        }
    }

    @Override
    protected void updateView() {
        //更新view
        if (setAnswerFriendAdapter != null) {
            for (MineFriendEntity mineFriendEntity : mineFriendEntities) {
                mineFriendEntity.isSelected = false;
            }
            showEmptyView();
            setAnswerFriendAdapter.updateDatas(mineFriendEntities);

        }
    }
}
